//
//  LTCommentCell.m
//  CodeExamples
//
//  Created by 乐天 on 2017/1/3.
//  Copyright © 2017年 cmsg. All rights reserved.
//

#import "LTCommentCell.h"
#import "MLLinkLabel.h"
#import "FriendCircleCommentModel.h"

@interface LTCommentCell ()<MLLinkLabelDelegate>
@property (nonatomic, strong) MLLinkLabel *likeLabel;
@property (nonatomic, strong) UIView *cutView;

@end

@implementation LTCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _likeLabel = [MLLinkLabel new];
        _likeLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_likeLabel];
        _likeLabel.numberOfLines = 0;
        _likeLabel.delegate = self;
        _likeLabel.font = [UIFont systemFontOfSize:14];
        _likeLabel.linkTextAttributes = @{NSForegroundColorAttributeName : RGBCOLOR(92,140,193)};
        [_likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(5);
            make.top.equalTo(self.contentView).offset(5);
            make.right.equalTo(self.contentView).offset(-5);
        }];
        
        
        UIView *cutView = [[UIView alloc] init];
        cutView.backgroundColor = [UIColor lightTextColor];
        _cutView = cutView;
        [self.contentView addSubview:cutView];
        
        [cutView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.likeLabel.mas_bottom).offset(5);
            make.left.right.equalTo(self.likeLabel);
            make.height.mas_equalTo(1);
            make.bottom.equalTo(self.contentView);
        }];
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}

- (void)cellDataWithModle:(FriendCircleCommentModel *)model andIshidden:(BOOL)hidden
{
    if (!model.attributedContent) {
        model.attributedContent = [self generateAttributedStringWithCommentItemModel:model];
    }
    _likeLabel.attributedText = model.attributedContent;
    _cutView.hidden = hidden;
}

///设置评论富文本
- (NSMutableAttributedString *)generateAttributedStringWithCommentItemModel:(FriendCircleCommentModel *)model
{
    NSString *text = model.firstUserName;
    if (model.secondUserName.length) {
        text = [text stringByAppendingString:[NSString stringWithFormat:@"回复%@", model.secondUserName]];
    }
    text = [text stringByAppendingString:[NSString stringWithFormat:@"：%@", model.commentString]];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:text];
    [attString setAttributes:@{NSLinkAttributeName : model.firstUserId} range:[text rangeOfString:model.firstUserName]];
    if (model.secondUserName) {
        [attString setAttributes:@{NSLinkAttributeName : model.secondUserId} range:[text rangeOfString:model.secondUserName]];
    }
    return attString;
}

#pragma mark - MLLinkLabelDelegate

- (void)didClickLink:(MLLink *)link linkText:(NSString *)linkText linkLabel:(MLLinkLabel *)linkLabel
{
    NSLog(@"%@", link.linkValue);
}

@end
